package com.yuanwushe.blue.onceandroid;

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.ParcelUuid;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.yuanwushe.blue.models.TelephoneUser;

import java.util.ArrayList;
import java.util.List;

public class TelephoneActivity extends AppCompatActivity {

    public RecyclerView mTelephoneRecycler;
    public TelephoneAdapter mAdapter;
    public List<TelephoneUser> mTelephoneUsers = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_telephone);
        init();
    }

    private void init(){
        mTelephoneRecycler = findViewById(R.id.telephone_list);
        mAdapter = new TelephoneAdapter(mTelephoneUsers);
        mTelephoneRecycler.setAdapter(mAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mTelephoneRecycler.setLayoutManager(linearLayoutManager);
        if (ContextCompat.checkSelfPermission(TelephoneActivity.this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 1);
        }
        else
        {
            readContacts();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 1: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    readContacts();
                }
                else {
                    Toast.makeText(this, "没有权限", Toast.LENGTH_SHORT).show();
                }
                break;
            }
            default:
        }
    }

    private void readContacts() {
        Cursor cursor = null;
        try{
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            if (cursor != null){
                while (cursor.moveToNext()) {
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    mTelephoneUsers.add(new TelephoneUser(name, number));
                }
                mAdapter.notifyDataSetChanged();
            }
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
        finally {
            if (cursor == null) {
                cursor.close();
            }
        }
    }

    class TelephoneAdapter extends RecyclerView.Adapter<TelephoneAdapter.TelephoneHolder>{

        public List<TelephoneUser> mData;

        public TelephoneAdapter(List<TelephoneUser> data){
            mData = data;
        }

        @Override
        public TelephoneHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.telephone_item, parent, false);
            return new TelephoneHolder(view);
        }

        @Override
        public void onBindViewHolder(TelephoneHolder holder, int position) {
            TelephoneUser item = mData.get(position);
            holder.Name.setText(item.getName());
            holder.Telephone.setText(item.getTelephone());
            holder.View.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        class TelephoneHolder extends RecyclerView.ViewHolder{
            public View View;
            public TextView Name;
            public TextView Telephone;
            public TelephoneHolder(View itemView) {
                super(itemView);
                View = itemView;
                Name = itemView.findViewById(R.id.name);
                Telephone = itemView.findViewById(R.id.telephone);
            }
        }
    }
}
